# load packages

pacman::p_load(tidyverse)


### table A.29(a) (matching estimates: full) ----

# load data
hamburg <- read_rds("data/hamburg_urnen_matching.rds")
berlin <- read_rds("data/berlin_urnen_matching.rds")
full <- bind_rows(hamburg, berlin)

# outcome
Y <- full$afd_share

# treatment indicator
Tr <- full$freibad

# matching variables
X <- full %>%
    dplyr::select(age_above_64, area, total, men, religion_catholic, religion_protestant,
                  birth_germany, birth_eu) %>%
    as.matrix()

# exact matching
by <- full$wahlbezirk

# match
full_urnen <- Matching::Matchby(Y = Y, by = by, Tr = Tr, X = X, M = 1, exact = F) 

summary(full_urnen)

# clean
rm(list = ls())

### table A.29(b) (matching estimates: berlin) ----

# load data
berlin <- read_rds("data/berlin_urnen_matching.rds")

# outcome
Y <- berlin$afd_share

# treatment indicator
Tr <- berlin$freibad

# matching variables
X <- berlin %>%
    dplyr::select(age_above_64, area, total, men, religion_catholic, religion_protestant,
                  birth_germany, birth_eu) %>%
    as.matrix()

# exact matching variable
by <- berlin$wahlbezirk

# perform matching
berlin_urnen <- Matching::Matchby(Y = Y, by = by, Tr = Tr, X = X, M = 1, exact = F) 

# result
summary(berlin_urnen)

# clean
rm(list = ls())

### table A.29(c) (matching estimates: hamburg) ----

# load data
hamburg <- read_rds("data/hamburg_urnen_matching.rds")

# outcome
Y <- hamburg$afd_share

# treatment indicator
Tr <- hamburg$freibad

# matching variables
X <- hamburg %>%
    dplyr::select(age_above_64, area, total, men, religion_catholic, religion_protestant,
                  birth_germany, birth_eu) %>%
    as.matrix()

# exact matching
by <- hamburg$wahlbezirk

# match
hamburg_urnen <- Matching::Matchby(Y = Y, by = by, Tr = Tr, X = X, M = 1, exact = F) 

summary(hamburg_urnen)

# clean
rm(list = ls())
